package com.lzh.persevere.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.persevere.common.BaseEntity;
import com.lzh.persevere.mapper.FileInfoMapper;
import com.lzh.persevere.mapper.MusicPlayerMapper;
import com.lzh.persevere.model.entity.FileInfo;
import com.lzh.persevere.model.entity.MusicPlayer;
import com.lzh.persevere.model.qo.music.MusicPageQO;
import com.lzh.persevere.model.vo.music.MusicPlayerVO;
import com.lzh.persevere.service.MusicPlayerService;
import com.lzh.persevere.template.PageAbstractTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 音乐播放器表服务实现类
 *
 * @author luzhiheng
 * @since 2025-07-12
 */
@Service
public class MusicPlayerServiceImpl extends ServiceImpl<MusicPlayerMapper, MusicPlayer> implements MusicPlayerService {

    @Resource
    private FileInfoMapper fileInfoMapper;

    @Resource
    private RedisCacheService redisCacheService;

    @Override
    public List<MusicPlayerVO> findMusicList(String moduleId) {
        List<MusicPlayer> musicPlayerList = baseMapper.selectList(
                Wrappers.<MusicPlayer>lambdaQuery().eq(MusicPlayer::getModuleId, moduleId).eq(MusicPlayer::getStatus, 1)
        );
        if (CollectionUtil.isEmpty(musicPlayerList)) {
            return new ArrayList<>();
        }
        List<Long> fileInfoIdList = Stream.concat(
                musicPlayerList.stream().map(MusicPlayer::getMusicFileInfoId),
                musicPlayerList.stream().map(MusicPlayer::getCoverFileInfoId)
        ).collect(Collectors.toList());

        List<FileInfo> fileInfoList = fileInfoMapper.selectList(
                Wrappers.<FileInfo>lambdaQuery().in(FileInfo::getId, fileInfoIdList)
        );
        Map<Long, String> fileIdMap = fileInfoList.stream().collect(Collectors.toMap(BaseEntity::getId, FileInfo::getFileAddress));
        return musicPlayerList.stream().map(e -> {
            MusicPlayerVO vo = BeanUtil.copyProperties(e, MusicPlayerVO.class);
            vo.setSrc(fileIdMap.get(e.getMusicFileInfoId()));
            vo.setCover(fileIdMap.get(e.getCoverFileInfoId()));
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public Map<String, Object> findMusicPage(MusicPageQO qo) {
        Map<String, Object> otherQueryMap = Collections.emptyMap();
        PageAbstractTemplate template = new PageAbstractTemplate() {
            @Override
            public Integer countTotal() {
                return baseMapper.countList(this.getQueryMap(qo.getPageModel(), otherQueryMap));
            }

            @Override
            public List<Map<String, Object>> queryPageList() {
                List<Map<String, Object>> pagedList = baseMapper.pageList(this.queryMap);
                if (CollectionUtil.isNotEmpty(pagedList)) {
                    Map<String, String> allUserIdNickNameMap = redisCacheService.getAllUserIdNickNameMap();
                    List<Long> fileInfoIdList = new ArrayList<>(pagedList.size() * 2);
                    for (Map<String, Object> map : pagedList) {
                        map.put("status1", "1".equals(map.get("status").toString()));
                        map.put("createNickName", allUserIdNickNameMap.get(String.valueOf(map.get("createUserId"))));
                        map.put("updateNickName", allUserIdNickNameMap.get(String.valueOf(map.get("updateUserId"))));
                        Long coverFileInfoId = Long.valueOf(map.get("coverFileInfoId").toString());
                        Long musicFileInfoId = Long.valueOf(map.get("musicFileInfoId").toString());
                        fileInfoIdList.add(coverFileInfoId);
                        fileInfoIdList.add(musicFileInfoId);
                    }
                    Map<Long, String> idUrlMap = fileInfoMapper.selectByIdList(fileInfoIdList).stream().collect(Collectors.toMap(BaseEntity::getId, FileInfo::getFileAddress));
                    for (Map<String, Object> map : pagedList) {
                        map.put("cover", idUrlMap.get(Long.valueOf(map.get("coverFileInfoId").toString())));
                        map.put("music", idUrlMap.get(Long.valueOf(map.get("musicFileInfoId").toString())));
                    }
                }
                return pagedList;
            }
        };
        return template.queryPage();
    }
}
